/**
 * 销售订单维护
 * @author liuw
 * @date 2019/09/09
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_gwax = {},
        cols = [[
            { type : "checkbox" , fixed: 'left'},
            { field: "GWCSMLEGAL_N1" , title: "销售组织名称", width: 100,fixed:'left'},
            { field: "GWCSDOCNO" , title: "报价单号", width: 180,sort:true,
                search:'default',searchOpts:{icon:true}, fixed:'left'},
            { field: "GWCSDATE" , title: "创建日期", width: 120,sort:true},
            { field: "GWCSDOCDATE" , title: "报价日期", width: 120,search: 'default',
                searchOpts: {type: 'daterange'},sort:true},
            { field: "GWCS011_N1" , title: "部门", width: 100},
            { field: "GWCS014_N1" , title: "业务员", width: 100},
            { field: "GWCS010" , title: "客户编号", sort:true, width: 150,search:"default",searchOpts: {icon: true}},
            { field: "GWAKL003" , title: "客户名称", sort:true, width: 100},
            { field: "GWCSSEQ" , title: "报价单项次", width: 100},
            { field: "GWCS001" , title: "料号", width: 150,sort:true},
            { field: "GWAML003" , title: "物料名称", width: 180},
            { field: "GWAML004" , title: "物料型号", width: 100},
            { field: "GWABL003" , title: "单位", width: 100},
            { field: "GWCS003" , title: "数量", width: 100},
            { field: "GWEA006" , title: "税率", width: 100},
            { field: "GWCS004" , title: "含税单价", width: 100},
            { field: "GWCS005" , title: "价税合计", width: 100},
            { field: "GWCS046" , title: "项目编码", width: 100,search: 'default', searchOpts: {icon: true}},
            { field: "GWEM002" , title: "项目名称", width: 100},
            { field: "GWAHL003" , title: "产品线名称", width: 100},
            { field: "GWCS060" , title: "客户订单号", width: 100},
            { field: "GWCS050" , title: "备注", width: 100},
            { field: "GWCS024_25" , title: "地市", width: 100},
            { field: "GWCS020" , title: "订单地址", width: 100},
            { field: "GWEC009_SUM" , title: "已出货数量", width: 100},
            { field: "GWEF009_SUM" , title: "已开票数量", width: 100},
            { field: "GWEF011_SUM" , title: "已开票金额", width: 100},
            { field: "GWEQ013_SUM" , title: "已回款金额", width: 100},
            { field: "WKPYSYE" , title: "未开票应收余额", width: 100},
            { field: "YKPYSYE" , title: "已开票应收余额", width: 100},
            { field: "YSYEHJ" , title: "应收余额合计", width: 100},
            { field: "GWCS045" , hide: true , width: 100},
            { field: "GWCS102" , hide: true , width: 100},
        ]],
        gweq023SettingAction
    ;

    var Gweq023 = app.Module.extend({
        init: function () {
            self = this;
            let searchArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gweq023',
                _searchs: [{
                    id: 'gweq023', tableId: 'gweq023_table',
                    datas: searchArr.pushArray([
                        {field: 'GWCS014', title: '业务员', hide: true, search: 'default', searchOpts: {icon: true}},
                        {field: 'GWCS011', title: '销售部门', hide: true, search: 'default', searchOpts: {icon: true}}
                    ])
                }],
                _loadTplsFull: [
                    {
                        dom: 'gweq023_ship', tpl: 'gweq023_ship'  // 0
                    },
                    {
                        dom: 'gweq023_check_accept', tpl: 'gweq023_check_accept' // 1
                    },
                    {
                        dom: 'gweq023_invoice', tpl: 'gweq023_invoice' // 2
                    },
                    {
                        dom: 'gweq023_payback', tpl: 'gweq023_payback' // 3
                    },
                    {
                        dom: 'common_open_window', tpl: 'common/_open_window' // 4
                    },
                ]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gweq023_table'),
                    url: g_ctxPath + 'crm/gweq023/getListData',
                    exportsUrl: g_ctxPath + 'crm/gweq023/exportListData',
                    exportsName: '销售订单数据',
                    title: '销售订单信息表',
                    method: 'post',
                    id: 'gweq023_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                //监听事件
                table.on('toolbar(gweq023_table)', function (obj) {
                    switch (obj.event) {
                        case 'ship':
                            self.mx("ship",0,"出货明细");
                            break;
                        case 'check_accept':
                            self.mx("check_accept",1,"验收明细");
                            break;
                        case 'invoice':
                            self.mx("invoice",2,"开票明细");
                            break;
                        case 'payback':
                            self.mx("payback",3,"回款明细");
                            break;
                    }
                });
            });
        },

        mx: function(tagName,_index,title) {
            var checkStatus = table.checkStatus('gweq023_table');
            var  data = checkStatus.data;
            if (data.length <= 0) {
                app.showWarning("请选择一笔数据进行操作");
                return;
            }
            if (data.length > 1) {
                app.showWarning("只能选择一笔数据进行操作");
                return;
            }
            var _xdata = {
                GWEC003 : data[0].GWCSDOCNO,
                GWEC101 : data[0].GWCSSEQ
            };
            self._openTplFull({
                tag: 'gweq023_' + tagName,
                tagData: _xdata,
                index: _index,
                openOpts: {
                    title: title,
                    area: ['800px', '500px']
                }
            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gweq023_table', where: table_gwax});
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gweq023',
                    watch: {
                        'isearch.fields.GWCSDOCDATE': function (newValue, oldValue) {
                            if (newValue != null && newValue.length == 2) {
                                this.isearch.fields.GWCSDOCDATE_START = newValue[0];
                                this.isearch.fields.GWCSDOCDATE_END = newValue[1];
                            } else {
                                this.isearch.fields.GWCSDOCDATE_START = '';
                                this.isearch.fields.GWCSDOCDATE_END = '';
                            }
                        }
                    },
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field === 'GWCSDOCNO') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwcsdocno',
                                            searchParam: {
                                                GWCSACTI: 'Y'
                                            },
                                            columnList: [
                                                {label: '报价单号', prop: 'GWCSDOCNO', width: '150'},
                                                {label: '报价日期', prop: 'GWCSDOCDATE', width: '150'},
                                                {label: '业务员', prop: 'GWCS014_NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCSDOCNO = data.GWCSDOCNO;
                                        }
                                    },
                                    index: 4,
                                    openOpts: {
                                        title: '选择分销订单号',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if (field == 'GWCS010') { //客户
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label:'客户编号',prop:'GWAK001', width: 150},
                                                {label:'客户简称',prop:'GWAKL003', width: 150},
                                                {label:'客户全称',prop:'GWAKL004', width: 150}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCS010 = data.GWAK001;
                                        }
                                    },
                                    index: 4,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field == 'GWCS014') {//业务员
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_sysuser_id',
                                            columnList: [
                                                {label: '业务员', prop: 'ID', width: '150'},
                                                {label: '业务员名称', prop: 'NAME', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCS014 = data.ID;
                                        }
                                    },
                                    index: 4,
                                    openOpts: {
                                        title: '选择业务员',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field == 'GWCS011') {//销售部门
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_sysorg_id',
                                            columnList: [
                                                {label: '部门编号', prop: 'ID', width: '150'},
                                                {label: '部门名称', prop: 'NAME', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCS011 = data.ID;
                                        }
                                    },
                                    index: 4,
                                    openOpts: {
                                        title: '选择销售部门',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field == 'GWCS046') {//项目
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwem001',
                                            columnList: [
                                                {label: '项目编号', prop: 'GWEM001', width: '150'},
                                                {label: '项目名称', prop: 'GWEM002', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCS046 = data.GWEM001;
                                        }
                                    },
                                    index: 4,
                                    openOpts: {
                                        title: '选择项目',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }
                    }

                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gweq023();

});